#include "modbuswig.h"
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    ui->setupUi(this);

    ui->led_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
    ui->fan_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
    ui->fmq_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
    ui->lignt_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");


    led_state = false;
    fan_state = false;
    voice_state = false;
    light_state = false;

}

ModbusWig::~ModbusWig()
{
    delete ui;
}

void ModbusWig::on_led_button_clicked()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);

    if(led_state == false)
    {
        QByteArray payload = "{\"LED\":\"on\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->led_button->setStyleSheet("QPushButton {background-color: rgba(128, 128, 128, 150);}");
        led_state = true;
    }
    else
    {
        QByteArray payload = "{\"LED\":\"off\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->led_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
        led_state = false;
    }
}

void ModbusWig::on_fan_button_clicked()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);

    if(fan_state == false)
    {
        QByteArray payload = "{\"FAN\":\"on\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->fan_button->setStyleSheet("QPushButton {background-color: rgba(128, 128, 128, 150);}");
        fan_state = true;
    }
    else
    {
        QByteArray payload = "{\"FAN\":\"off\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->fan_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
        fan_state = false;
    }
}

void ModbusWig::on_lignt_button_clicked()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);

    if(light_state == false)
    {
        QByteArray payload = "{\"LIGHT\":\"on\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->lignt_button->setStyleSheet("QPushButton {background-color: rgba(128, 128, 128, 150);}");
        light_state = true;
    }
    else
    {
        QByteArray payload = "{\"LIGHT\":\"off\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->lignt_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
        light_state = false;
    }
}

void ModbusWig::on_fmq_button_clicked()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);

    if(voice_state == false)
    {
        QByteArray payload = "{\"VOICE\":\"on\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->fmq_button->setStyleSheet("QPushButton {background-color: rgba(128, 128, 128, 150);}");
        voice_state = true;
    }
    else
    {
        QByteArray payload = "{\"VOICE\":\"off\"}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);

        ui->fmq_button->setStyleSheet("QPushButton {background: transparent;background-color: rgba(255, 255, 255, 50);}");
        voice_state = false;
    }
}
